<script setup name="Product">
import { productGetList } from "@/api/wms/product";
import ProductCurd from "@/views/wms/product/curd";
import DictTreeLeft from "@/views/sys/dict/treeLeft";
import { getCurrentInstance, onMounted, reactive } from "vue";
import { useRoute } from "vue-router";
const { proxy } = getCurrentInstance();
const { sys_is } = proxy.useDict('sys_is');
const route = useRoute();

onMounted(() => {
  pd.queryParams.isStock = route.query.isStock;
  // proxy.$refs["dictLeftTreeRef"].initDictLeftTree();
  getList();
  // treeGetList({ancestors: route.query.ancestors}).then(res => {
  //   pd.treeList = proxy.handleTree(res.data, 'id', 'parentId', 'children');
  // });
});

const pd = reactive({
  productList: [],
  loading: true,
  showSearch: true,
  total: 0,
  daterangeUpdate_time: [],
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    treeId: null,
    specification: null,
    attrValue: null,
    isStock: route.query.isStock,
    enableFlag: "0"
  },

});

/** 查询合作伙伴列表 */
function getList() {
  pd.loading = true;
  if (null != pd.daterangeUpdate_time && '' != pd.daterangeUpdate_time) {
    pd.queryParams.beginTime = pd.daterangeUpdate_time[0];
    pd.queryParams.endTime = pd.daterangeUpdate_time[1];
  }
  productGetList(pd.queryParams).then(res => {
    pd.productList = res.data;
    pd.total = res.total;
    pd.loading = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  pd.queryParams.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  pd.queryParams.beginTime = null;
  pd.queryParams.endTime = null;
  pd.queryParams.name = null;
  pd.queryParams.treeId = null;
  pd.daterangeUpdate_time = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  proxy.$refs["productCurdRef"].add();
}

/** 修改按钮操作 */
function handleEdit(product) {
  proxy.$refs["productCurdRef"].edit(product);
}

/** 删除按钮操作 */
function handleDel(id) {
  proxy.$refs["productCurdRef"].del(id);
}

/** 对话框完成后刷新界面 */
function productCurdOk(operation) {
  getList();
}

function treeNodeClick(tree) {
  pd.queryParams.treeId = tree.id;
  getList();
}

</script>

<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!-- 系统树数据-->
      <el-col :span="4" :xs="24">
        <DictTreeLeft v-if="pd.queryParams.isStock === '1'" ref="dictLeftTreeRef" dict="wms_product_service"
          @ok="treeNodeClick" />
        <DictTreeLeft v-if="pd.queryParams.isStock === '0'" ref="dictLeftTreeRef" dict="wms_product_stock"
          @ok="treeNodeClick" />
        <!--        <TreeLeft :treeList="pd.treeList" />-->
      </el-col>

      <el-col :span="20" :xs="24">
        <el-form :model="pd.queryParams" ref="queryRef" :inline="true" v-show="pd.showSearch" label-width="68px">
          <el-form-item prop="name">
            <el-input v-model="pd.queryParams.name" placeholder="请输入搜索产品名称" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item prop="specification">
            <el-input v-model="pd.queryParams.specification" placeholder="请输入搜索产品规格" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item prop="enableFlag">
            <el-switch v-model="pd.queryParams.enableFlag" active-text="启用" inactive-text="停用" active-value="0"
              inactive-value="1"></el-switch>
          </el-form-item>
          <el-form-item label="更新时间" style="width: 308px">
            <el-date-picker v-model="pd.daterangeUpdate_time" value-format="YYYY-MM-DD" type="daterange"
              range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="Search" @click="handleQuery">搜 索</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button icon="Refresh" @click="resetQuery">重 置</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['wms:product:add']">新 增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="info" plain icon="Upload" v-hasPermi="['wms:product:add']">导 入</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" v-hasPermi="['wms:product:export']">导 出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="pd.showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="pd.loading" :data="pd.productList">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="名称" align="center" prop="name">
            <template #default="scope">
              <el-link type="primary" @click="handleEdit(scope.row)" v-hasPermi="['wms:product:edit']">
                {{ scope.row.name }}
              </el-link>
            </template>
          </el-table-column>
          <el-table-column label="价格" align="center" prop="price" />
          <el-table-column label="属性" align="center" prop="attrValue" />
          <el-table-column label="规格型号" align="center" prop="specification" />
          <el-table-column label="计量单位" align="center" prop="measureUnit" />
          <el-table-column label="包装单位" align="center" prop="packingUnit" />
          <el-table-column label="包装数量" align="center" prop="packingQuantity" />
          <el-table-column label="是否启用" align="center" prop="enableFlag">
            <template #default="scope">
              <dict-tag :options="sys_is" :value="scope.row.enableFlag" />
            </template>
          </el-table-column>
          <el-table-column label="备注" align="center" prop="remark" />
          <el-table-column label="创建人" align="center" prop="createBy" />
          <el-table-column label="创建时间" align="center" prop="createTime" width="180">
            <template #default="scope">
              <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="更新人" align="center" prop="updateBy" />
          <el-table-column label="更新时间" align="center" prop="updateTime" width="220">
            <template #default="scope">
              <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width" width="180">
            <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="handleEdit(scope.row)" v-hasPermi="['wms:product:edit']">
                修改
              </el-button>
              <el-button link type="primary" icon="Delete" @click="handleDel(scope.row.id)"
                v-hasPermi="['wms:product:del']"> 删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="pd.total > 0" :total="pd.total" v-model:page="pd.queryParams.pageNum"
          v-model:limit="pd.queryParams.pageSize" @pagination="getList" />
      </el-col>
    </el-row>
    <ProductCurd ref="productCurdRef" @ok="productCurdOk" />
  </div>
</template>

<style lang='scss' scoped>
.el-form-item {
  margin-bottom: 18px
}
</style>